package com.scalar.forecast.forecastSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastSetup.forms.ForecastSetupForm;
import com.scalar.forecast.forecastSetup.ForecastTimeFrame;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DBXException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * User: Sujan Kumar
 * Date: Feb 9, 2005
 * Time: 9:50:07 AM
 */
public class ForecastSetupConfirmCommand extends ActionCommand
{
    public ActionForward execute ( ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response )
    {
        ForecastSetupForm forecastSetupForm = ( ForecastSetupForm )form;
        HashMap errors = new HashMap ();
        HashMap info = new HashMap ();
        DBTransaction dbTransaction = null;
        boolean success = false;
        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();

            ForecastSetupDAO.updateForecastEntry( forecastSetupForm.isByProduct(), forecastSetupForm.isByCustomer(), forecastSetupForm.isByProject(), forecastSetupForm.isByOdm(), dbTransaction );
            ForecastTimeFrame forecastTimeFrame = new ForecastTimeFrame();
            forecastTimeFrame.setTfName( forecastSetupForm.getTfName() );
            forecastTimeFrame.setTfCompany( forecastSetupForm.getTfCompany() );
            forecastTimeFrame.setTfMeasureType( forecastSetupForm.getTfMeasureType() );
            success = ForecastSetupDAO.updateForecastTimeFrame( forecastTimeFrame, dbTransaction );
            if ( !success )
            {
                errors.put ( ERROR_COULD_NOT_UPDATE_FORECAST_TIMEFRAME, null );
                info.put ( FORECAST_SETUP_SCREEN_LABEL, INFO_SAVE_FAILED );
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
                request.setAttribute( SessionKeys.INFO_MESSAGES, info );
                return mapping.findForward ( "forecastSetupConfirm" );
                //throw new Exception ( "Could Not Update ForecastTimeFrame" );
            }
            success = ForecastSetupDAO.saveForecastLevels( forecastSetupForm.getLevels(), dbTransaction );
            if ( !success )
            {
                errors.put ( ERROR_COULD_NOT_SAVE_FROECAST_LEVELS, null );
                info.put ( FORECAST_SETUP_SCREEN_LABEL, INFO_SAVE_FAILED );
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
                request.setAttribute( SessionKeys.INFO_MESSAGES, info );
                return mapping.findForward ( "forecastSetupConfirm" );
                //throw new Exception ( "Could Not Save ForecastLevels" );
            }
            success = ForecastSetupDAO.updateForecastEntryFiels( forecastSetupForm.getForecastEntryFields(), dbTransaction );
            if ( !success )
            {
                errors.put ( ERROR_COULD_NOT_UPDATE_FORECAST_ENTRY_FIELDS, null );
                info.put ( FORECAST_SETUP_SCREEN_LABEL, INFO_SAVE_FAILED );
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
                request.setAttribute( SessionKeys.INFO_MESSAGES, info );
                return mapping.findForward ( "forecastSetupConfirm" );
                //throw new Exception ( "Could Not Update ForecastEntryFields" );
            }

            info.put ( FORECAST_SETUP_SCREEN_LABEL + " " + INFO_SAVE_SUCCESSFULL , null );

            request.setAttribute( SessionKeys.INFO_MESSAGES, info );
            //System.out.println ( "------ForecastSetupSave Successfull-----------" );
        }
        catch ( DBXException e )
        {
            success = false;
            //System.out.println ( "---Exception while Creating DBTransaction---" );
            e.printStackTrace();
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_CREATE_TRANSACTION );
            info.put ( FORECAST_SETUP_SCREEN_LABEL, INFO_SAVE_FAILED );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            request.setAttribute( SessionKeys.INFO_MESSAGES, info );
            return mapping.findForward ( "forecastSetupConfirm" );

        }
        catch ( Exception e )
        {
            success = false;
            System.out.println ( "Exception:" + e.getMessage() );
            e.printStackTrace();
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_SAVE_FORECAST_SETUP_SCREEN );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            return mapping.findForward ( "forecastSetupConfirm" );
        }
        finally
        {
            if ( dbTransaction != null )
            {
                try
                {
                    if ( success )
                    {
                        dbTransaction.commit();
                    }
                    else
                    {
                        dbTransaction.rollback();
                    }
                }
                catch ( Exception  ignore ){ }
            }
        }
        ActionForward actionForward = mapping.findForward( "forecastSetupConfirm" );
        return actionForward;
    }
}
